using System;
using Spring.Objects.Factory;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;

namespace Liberate.CodeGenerator.Spring.DynamicLoading
{
    class ObjectFactory : DefaultListableObjectFactory
    {
        public ObjectFactory(bool caseSensitive, IObjectFactory parentFactory)
            : base(caseSensitive, parentFactory)
        {
        }

        protected override RootObjectDefinition GetMergedObjectDefinition(string name, IObjectDefinition definition)
        {
            var rootDefinition = base.GetMergedObjectDefinition(name, definition);
            if (rootDefinition != null)
            {
                Type type;
                if (rootDefinition.HasObjectType)
                {
                    type = AssemblyUtils.ResolveType(rootDefinition.ObjectType.AssemblyQualifiedName);
                }
                else
                {
                    type = AssemblyUtils.ResolveType(rootDefinition.ObjectTypeName);
                }

                if (type != null)
                {
                    rootDefinition.ObjectType = type;
                }
            }
            return rootDefinition;
        }        
    }
}